package com.hong.spring.service;

import com.google.common.net.HostAndPort;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.orbitz.consul.AgentClient;
import com.orbitz.consul.Consul;
import com.orbitz.consul.HealthClient;
import com.orbitz.consul.model.agent.ImmutableRegCheck;
import com.orbitz.consul.model.agent.ImmutableRegistration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * @author: csh
 * @Date: 2020/12/22 16:05
 * @Description:
 */
@Configuration
public class IConsulConfig {



    private  String consulInfo;

    private  String consulSecond;

    private  String consulUrl;

    private String consulServiceName;

    private String consulServiceId;

    private String versionTag;

    private String consulServiceHost;

    private Integer servicePort;
    private Consul consul;

    public String getConsulInfo() {
        return consulInfo;
    }

    public void setConsulInfo(String consulInfo) {
        this.consulInfo = consulInfo;
    }

    public String getConsulSecond() {
        return consulSecond;
    }

    public void setConsulSecond(String consulSecond) {
        this.consulSecond = consulSecond;
    }

    public String getConsulUrl() {
        return consulUrl;
    }

    public void setConsulUrl(String consulUrl) {
        this.consulUrl = consulUrl;
    }

    public String getConsulServiceName() {
        return consulServiceName;
    }

    public void setConsulServiceName(String consulServiceName) {
        this.consulServiceName = consulServiceName;
    }

    public String getConsulServiceId() {
        return consulServiceId;
    }

    public void setConsulServiceId(String consulServiceId) {
        this.consulServiceId = consulServiceId;
    }

    public String getVersionTag() {
        return versionTag;
    }

    public void setVersionTag(String versionTag) {
        this.versionTag = versionTag;
    }

    public String getConsulServiceHost() {
        return consulServiceHost;
    }

    public void setConsulServiceHost(String consulServiceHost) {
        this.consulServiceHost = consulServiceHost;
    }

    public Integer getServicePort() {
        return servicePort;
    }

    public void setServicePort(Integer servicePort) {
        this.servicePort = servicePort;
    }

    public Consul getConsul() {
        return consul;
    }

    public void setConsul(Consul consul) {
        this.consul = consul;
    }

    public IConsulConfig(String consulInfo) {
        this.consulInfo = consulInfo;
        consul = Consul.builder().withHostAndPort(HostAndPort.fromString(consulInfo)).build();
    }

    public IConsulConfig() {
    }



    /**
     * 服务注册
     */
    @Bean
    public  AgentClient serviceRegister() {
        AgentClient agent = consul.agentClient();

        //健康检测
        ImmutableRegCheck check = ImmutableRegCheck.builder().http(consulUrl).interval(consulSecond).build();

        ImmutableRegistration.Builder builder = ImmutableRegistration.builder();
        builder.id(consulServiceId).name(consulServiceName).addTags(versionTag).address(consulServiceHost).port(servicePort).addChecks(check);

        agent.register(builder.build());
        return agent;
    }

    /**
     * 服务获取
     */
    @Bean
    public  HealthClient serviceGet() {
        HealthClient client = consul.healthClient();
        //获取所有服务
        System.out.println(client.getAllServiceInstances(consulServiceName).getResponse().size());

        //获取所有正常的服务（健康检测通过的）
        client.getHealthyServiceInstances(consulServiceName).getResponse().forEach((resp) -> {
            System.out.println("获取到的正常服务："+resp);
        });
        return client;
    }
}
